<template>
    <div class="comment-list">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多评论了" @load="onLoad" :error.sync="error"
            error-text="请求失败，点击重新加载">
            <!-- <van-cell v-for="item in list" :key="item.com_id" :title="item.content" /> -->
            <CommentItem v-for="item in list" :key="item.com_id" :comment="item"
                @replyClick="$emit('onReplyClick', $event)"></CommentItem>
        </van-list>
    </div>
</template>

<script>
import { getComments } from '../../../api/comment'
import CommentItem from '../components/comment-item.vue'

export default {
    name: 'ArticleComment',
    props: {
        source: {
            type: [Number, String, Object],
            required: true,
        },
        list: {
            type: Array,
            default: () => []
        },
        type: {
            type: String,
            default: 'a',
            validator(value) {
                return ['a', 'c'].includes(value)
            }
        }
    },
    data() {
        return {
            // list: [],
            loading: false,
            finished: false,
            offset: null,
            limit: 10,
            error: false,
        };
    },
    methods: {
        async onLoad() {
            try {
                // if (Math.random() > 0.2) {
                //     JSON.parse('abc')
                // }
                const { data } = await getComments({
                    type: this.type,
                    source: this.source,
                    offset: this.offset,
                    limit: this.limit
                })
                const { results } = data.data
                this.list.push(...results)
                this.loading = false
                if (results.length) {
                    this.offset = data.data.last_id
                } else {
                    this.finished = true
                }
            } catch (err) {
                console.log('请求失败', err);
                this.error = true
                this.loading = false
            }
        }
    },
    components: {
        CommentItem
    }
}
</script>

<style>
</style>